package com.eightbears.beboy.ui.merchant.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.eightbears.beboy.R;
import com.eightbears.beboy.base.BaseActivity;
import com.eightbears.beboy.base.util.CommonUtils;
import com.eightbears.beboy.base.util.GsonUtil;
import com.eightbears.beboy.base.util.SPUtil;
import com.eightbears.beboy.base.util.ShowToast;
import com.eightbears.beboy.base.util.okhttp.OkHttpUtils;
import com.eightbears.beboy.base.util.okhttp.callback.StringCallback;
import com.eightbears.beboy.base.view.SwipeBackLayout;
import com.eightbears.beboy.entity.ValidOrderEntity;
import com.eightbears.beboy.entity.status.Status_Error;
import com.pnikosis.materialishprogress.ProgressWheel;
import com.socks.library.KLog;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

import static com.eightbears.beboy.base.util.CommonAPI.CONNECTION_STATUS_SUCCESS;
import static com.eightbears.beboy.base.util.CommonAPI.KEY_HTTP_HEADER;
import static com.eightbears.beboy.base.util.CommonAPI.STATUS_APPLY_REFOUND;
import static com.eightbears.beboy.base.util.CommonAPI.STATUS_END_DATE;
import static com.eightbears.beboy.base.util.CommonAPI.STATUS_REFOUND_SUCCESS;
import static com.eightbears.beboy.base.util.CommonAPI.STATUS_USE;
import static com.eightbears.beboy.base.util.CommonAPI.URL_SHOP_CODE;
import static com.eightbears.beboy.ui.merchant.activity.ShopManageActivity.PARAMS_VALID_CODE;

public class ShopValidCodeActivity extends BaseActivity {

    @BindView(R.id.progress_wheel)
    ProgressWheel progress_wheel;
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.tv_order_title)
    TextView tv_order_title;
    @BindView(R.id.tv_price)
    TextView tv_price;
    @BindView(R.id.tv_date)
    TextView tv_date;
    @BindView(R.id.ll_fail)
    LinearLayout ll_fail;
    @BindView(R.id.ll_success)
    LinearLayout ll_success;
    @BindView(R.id.tv_fail_info)
    TextView tv_fail_info;
    @BindView(R.id.tv_revalid)
    TextView tv_revalid;
    @BindView(R.id.sw_finish)
    SwipeBackLayout sw_finish;

    private String code;
    private String failInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop_valid_code);

        initView();
        initData();
        initEvent();
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        tv_title.setText("订单验证");
    }

    @Override
    protected void initData() {
        code = getIntent().getStringExtra(PARAMS_VALID_CODE);
        getOderInfo();
    }

    @Override
    protected void initEvent() {
        sw_finish.setCallBack(new SwipeBackLayout.CallBack() {
            @Override
            public void onFinish() {
                finish();
            }
        });
    }

    @OnClick(R.id.tv_submit_order)
    void submitOrder() {
        progress_wheel.setVisibility(View.VISIBLE);
        KLog.e(code);
        validCode();
    }

    @OnClick(R.id.tv_cancel)
    void cancelOrder() {
        finish();
    }

    @OnClick(R.id.ll_back)
    void back() {
        finish();
    }

    @OnClick(R.id.tv_revalid)
    void reValid() {
        finish();
    }

    private void getOderInfo() {
        String url = URL_SHOP_CODE + code;
        OkHttpUtils.get().url(url)
                .addHeader(KEY_HTTP_HEADER, "Bearer " + SPUtil.getAccessToken())
                .build().execute(new StringCallback() {
            @Override
            public void onResponse(String response) {
                progress_wheel.setVisibility(View.GONE);
                ValidOrderEntity validOrderEntity = GsonUtil.fromJson(response, ValidOrderEntity.class);

                if (validOrderEntity.getStatus_code().equals(CONNECTION_STATUS_SUCCESS)) {
                    if (null != validOrderEntity.getData()) {
                        if(validOrderEntity.getData().getStatus().equals(STATUS_USE)) {
                            failInfo = "此订单已经使用, 请核对后再试";
                            showFailInfo();
                        } else if(validOrderEntity.getData().getStatus().equals(STATUS_END_DATE)) {
                            failInfo = "此订单已经过期, 请核对后再试";
                            showFailInfo();
                        } else if(validOrderEntity.getData().getStatus().equals(STATUS_APPLY_REFOUND)){
                            failInfo = "此订单已申请退款, 请核对后再试";
                            showFailInfo();
                        } else if(validOrderEntity.getData().getStatus().equals(STATUS_REFOUND_SUCCESS)) {
                            failInfo = "此订单已退款, 请核对后再试";
                            showFailInfo();
                        } else {
                            showSuccessInfo();
                            String price = String.format(getString(R.string.text_price), validOrderEntity.getData().getCoupon().getDiscount_price());
                            tv_price.setText(price);
                            tv_date.setText(CommonUtils.TimeStamp2Date(validOrderEntity.getData().getCoupon().getEnd_at(), "yyyy-MM-dd"));
                            tv_order_title.setText(validOrderEntity.getData().getCoupon().getTitle());
                        }

                    } else {
                        failInfo = "此订单无效, 请核对后再试";
                        showFailInfo();
                    }
                }
            }

            @Override
            public void onError(Call call, Exception e) {
                progress_wheel.setVisibility(View.GONE);
                Status_Error status_error = GsonUtil.fromJson(e.getMessage(), Status_Error.class);
                if (status_error.getStatus_code() == 40101) {
                    failInfo = "此套餐不属于你店,请核对后再试";
                } else {
                    failInfo = "此订单已经使用或者无效, 请核对后再试";
                }
                showFailInfo();
                finish();
            }
        });
    }

    private void showFailInfo() {
        ll_success.setVisibility(View.GONE);
        ll_fail.setVisibility(View.VISIBLE);
        tv_fail_info.setText(failInfo);
    }

    private void showSuccessInfo() {
        ll_success.setVisibility(View.VISIBLE);
        ll_fail.setVisibility(View.GONE);
    }

    /**
     * 提交验证
     */
    private void validCode() {
        String url = URL_SHOP_CODE + code + "/verification";

        OkHttpUtils.put().url(url)
                .addHeader(KEY_HTTP_HEADER, "Bearer " + SPUtil.getAccessToken())
                .build().execute(new StringCallback() {
            @Override
            public void onResponse(String response) {
                progress_wheel.setVisibility(View.GONE);
                ShowToast.showShortToast("订单使用成功");
                finish();
            }

            @Override
            public void onError(Call call, Exception e) {
                progress_wheel.setVisibility(View.GONE);
                Status_Error status_error = GsonUtil.fromJson(e.getMessage(), Status_Error.class);
                if (status_error.getStatus_code() == 40101) {
                    failInfo = "此套餐不属于你店,请核对后再试";
                } else {
                    failInfo = "此订单已经使用或者无效, 请核对后再试";
                }
                showFailInfo();
            }
        });
    }

}
